﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace 高频打印
{
    public partial class FormDepart : Form
    {
        public FormDepart()
        {
            InitializeComponent();
        }
            MySqlConnection conn = new MySqlConnection("data source=localhost;database=test;user id=root;password=root;pooling=false;charset=utf8");

            private void Cleartext()
            {
                comboBox1.Items.Clear();
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";

                string sql = "select DepartID from Depart_Table order by DepartID";
                DataTable dtShow = new DataTable();
                MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
                sda.Fill(dtShow);
                if (dtShow.Rows.Count > 0)
                {
                    for (int i = 0; i < dtShow.Rows.Count; i++)
                    {
                        comboBox1.Items.Add(dtShow.Rows[i][0]);

                    }
                        comboBox1.SelectedIndex = -1;
                }
                

            }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "添 加")
            {
                button1.Text = "确定添加";
                button2.Text = "返 回";
                button3.Enabled = false;
                comboBox1.Visible = false;
                textBox3.Visible = true;
                textBox1.Enabled = true;
                textBox2.Enabled = true;


            }

            else if (button1.Text == "确定添加")
            {


                if (textBox1.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("请输入“门店名称”再进行添加");
                    return;

                }
                if (textBox3.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("请输入“门店编码”再进行添加");
                    return;

                }
                if (conn.State == ConnectionState.Open)
                {
                }
                else
                {
                    conn.Open();
                }
                string sql = "Insert into Depart_Table(DepartID,DepartName,DepartNote)Values('" + textBox3.Text + "','" + textBox1.Text + "','" + textBox2.Text + "')";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                int ret = cmd.ExecuteNonQuery();
                MessageBox.Show("数据添加成功");
                Cleartext();
                


                //调用添加
                //调用清除
                //调用Combobox1数据添加
                button1.Text = "添 加";
                button2.Text = "修 改";
                button3.Enabled = false;
                comboBox1.Visible = true;
                textBox3.Visible = false;
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }


            else if (button1.Text == "确定修改")
            {
                if (textBox1.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("请输入“门店名称”再进行数据修改");
                    return;

                }
                
                if (conn.State == ConnectionState.Open)
                {
                }
                else
                {
                    conn.Open();
                }
                string sql = "update depart_Table set DepartName = '" + textBox1.Text + "',DepartNote = '" + textBox2.Text + "' where departID = '" + comboBox1.Text  +"'";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                int ret = cmd.ExecuteNonQuery();
                MessageBox.Show("数据修改成功");
                Cleartext();


                //调用修改
                //调用清除
                //调用Combobox1数据添加
                button1.Text = "添 加";
                button2.Text = "修 改";
                button3.Enabled = false;
                comboBox1.Visible = true;
                comboBox1.Enabled = true;
                textBox3.Visible = false;
                textBox1.Enabled = false;
                textBox2.Enabled = false;


            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "修 改")
            {
                button1.Text = "确定修改";
                button2.Text = "返 回";
                button3.Enabled = false;
                comboBox1.Visible = true;
                textBox3.Visible = false;
                comboBox1.Enabled = false;
                textBox1.Enabled = true;
                textBox2.Enabled = true;
   
            }

            else if (button2.Text == "返 回")
            {
                button1.Text = "添 加";
                button2.Text = "修 改";
                comboBox1.Enabled = true;
                comboBox1.Visible = true;
                textBox3.Visible = false;
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                Cleartext();
            }
        }

        private void FormDepart_Load(object sender, EventArgs e)
        {
            string sql = "select DepartID from Depart_Table order by DepartID";
            DataTable dtShow = new DataTable();
            MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
            sda.Fill(dtShow);
            if (dtShow.Rows.Count > 0)
            {
                for (int i = 0; i < dtShow.Rows.Count; i++)
                {
                    comboBox1.Items.Add(dtShow.Rows[i][0]);

                }
            }

            else
            {
                MessageBox.Show("还没有部门编码数据，请点击“添加”按钮进行数据维护");
            }
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {


            }
            else

            {
                string sql = "select * from Depart_Table where departID='" + comboBox1.Text + "'";
                DataTable dtShow = new DataTable();
                MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
                sda.Fill(dtShow);
                textBox1.Text = dtShow.Rows[0][1].ToString();
                textBox2.Text = dtShow.Rows[0][2].ToString();
                button2.Enabled = true;
                button3.Enabled = true; 
            }

        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要删除该笔资料？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
            {
                if (conn.State == ConnectionState.Open)
                {
                }
                else
                {
                    conn.Open();
                }
                
                string sql = "delete from Depart_Table where departID = '" + comboBox1.Text + "'";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                int ret = cmd.ExecuteNonQuery();
                MessageBox.Show("数据删除成功");
                Cleartext();

            }
                
                // Application.Exit(); 
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit(); 
        }
    }
}
